/*
 * @Author: ZhangXiaodong
 * @Date: 2025-04-15 00:01:54
 * @LastEditors: ZhangXiaodong
 * @LastEditTime: 2025-04-26 00:11:33
 * @Description: 获取详情
 * @FilePath: /blog-ost/src/controller/blog/getDetail.js
 */

const mysqlPool = require('../../db/mysql/db.pool');
const logger = console; // 可替换为专业日志库，如 winston

/**
 * 根据博客 ID 获取博客详情
 * @param {number} id - 博客的 ID
 * @returns {Promise<Object|null>} - 博客详情对象，如果未找到则返回 null
 */
const getDetail = async (id) => {
    // 参数验证
    if (typeof id !== 'number' || isNaN(id)) {
        logger.error(`无效的博客 ID: ${id}`);
        return null;
    }

    const sql = `SELECT *,DATE_FORMAT(createTime,'%y-%m-%d %H:%i:%s') as createTime  FROM blogs WHERE id = ?`;
    try {
        const res = await mysqlPool.exec(sql, [id]);
        return res[0] || null;
    } catch (error) {
        logger.error(`获取博客详情时出错，博客 ID: ${id}，错误信息: ${error.message}`);
        return null;
    }
};

module.exports = getDetail;